package com.scau.yangsuiyu.estore.controller.buyer.manageBasicInfo;

import com.scau.yangsuiyu.estore.controller.myComponent.DeliveryAddressHBox;
import com.scau.yangsuiyu.estore.controller.buyer.EditDeliveryAddressPane;
import com.scau.yangsuiyu.estore.dababases.dao.BuyerDao;
import com.scau.yangsuiyu.estore.dababases.entity.Buyer;
import com.scau.yangsuiyu.estore.dababases.entity.DeliveryAddress;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Set;

/**
 * @author suiyu_yang
 * @description 管理买家收货地址
 * @date 2019-06-09 08:49
 * @email suiyu_yang@163.com
 */
//@FXMLController
public class InfoDeliveryAddressPane extends AnchorPane implements Initializable {
    private Buyer buyer;

    @FXML
    private VBox deliveryAddressVBox;

    private List<HBox> deliveryAddressHBoxList = new ArrayList<>();

    @Deprecated
    public InfoDeliveryAddressPane() {
        this.buyer = new Buyer();
        loadFXML();
    }

    public InfoDeliveryAddressPane(Buyer buyer) {
        this.buyer = buyer;
        loadFXML();
    }

    private void loadFXML() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/buyer/manageBasicInfo/InfoDeliveryAddressPane.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);
        try {
            fxmlLoader.load();
            updateAddress();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

    @FXML
    private void addAddressClick() {
        if (buyer.getDeliveryAddresses().size() == 5) {
            new Alert(Alert.AlertType.ERROR, "最多只能保存五个收货地址！").showAndWait();
        } else {
            EditDeliveryAddressPane editDeliveryAddressPane = new EditDeliveryAddressPane(this);
            editDeliveryAddressPane.showPane();
        }
    }


    public void updateAddress() {
        buyer = new BuyerDao().getBuyerByUsername(buyer.getUsername());
        Set<DeliveryAddress> deliveryAddresses = buyer.getDeliveryAddresses();

        deliveryAddressVBox.getChildren().removeAll(deliveryAddressHBoxList);
        deliveryAddressHBoxList.clear();


        for (DeliveryAddress deliveryAddress : deliveryAddresses) {
            DeliveryAddressHBox deliveryAddressHBox = new DeliveryAddressHBox(deliveryAddress);
            deliveryAddressHBox.setInfoDeliveryAddressPane(this);
            deliveryAddressHBoxList.add(deliveryAddressHBox);
        }

        deliveryAddressVBox.getChildren().addAll(deliveryAddressHBoxList);
    }

    public Buyer getBuyer() {
        return buyer;
    }
}
